const { config } = require("./config")
import {LoginModel} from './model/login'
import {OwnModel} from './model/own'
let login = new LoginModel()
var own = new OwnModel()
//app.js
App({
  onLaunch: function (options) {
    console.log("##################################")
    console.log(options.query)
    console.log("##################################")
    var that = this;
    var userId = 0;
    if(options.query.id){
      userId = options.query.id
    } else if(options.query.scene){
      let scene=decodeURIComponent(options.query.scene);
      userId = scene.split(";;")[1]
    }
    wx.getSystemInfo({
      success (res) {
        if(res.system.indexOf('iOS') === 0){
          config.system = "ios"
        }
        else{
          config.system = "andriod"
        }
        config.statusBarHeight = res.statusBarHeight - 20
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log(res)
              // 可以将 res 发送给后台解码出 unionId
              that.globalData.userInfo.nickName = res.userInfo.nickName
              that.globalData.userInfo.avatarUrl = res.userInfo.avatarUrl
              that.globalData.userInfo.gender = res.userInfo.gender
              that.globalData.userInfo.province = res.userInfo.province
              that.globalData.userInfo.city = res.userInfo.city
              that.globalData.userInfo.country = res.userInfo.country
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
      // 登录
      wx.login({
        success: res => {
          if (res.code) {
            console.log(res.code)
            that.globalData.code = res.code
            // 发起网络请求
            login.loginCode(res.code,userId).then(
              resp=>{
                console.log(resp)
                if (resp.code===0) {
                  config.token = resp.data.token||""
                  config.isMobile = resp.data.isMobile || "0"
                  that.globalData.sKey = resp.data.sessionKey || ""
                  own.info().then(
                    res=>{
                      if (res.code===0) {
                        that.globalData.userInfo.id = res.data.userId
                        that.globalData.userInfo.nickName = res.data.nickName
                        that.globalData.userInfo.avatarUrl = res.data.picture
                      }
                    }
                  )
                }
              },
            )
          } else {
            console.log('登录失败！' + res.errMsg)
          }
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
        },
      })
  },
  globalData: {
    code:'',
    sKey:'',
    userInfo: {
      id:'',
      nickName:'',
      avatarUrl:'',
      gender:'',
      province:'',
      city:'',
      country:'',
      mobileKey:'',
      mobile:''
    }
  }
})